import { getTodo } from '../api/todo/index'
import axios from '../api/axios'
import { Dialog } from 'vant'

// roleCode 获取当前岗位
export const getRoleCode = function (userInfo: any) {
  const postName = userInfo.postName || ''
  let roleCode: string = ''
  if (
    postName.includes('学生') ||
    postName.includes('班长') ||
    postName.includes('学习委员')
  ) {
    roleCode = 'student'
  } else if (postName.includes('家长')) {
    roleCode = 'parent'
  } else if (
    postName.includes('老师') ||
    postName.includes('咨询师') ||
    postName.includes('专家') ||
    postName.includes('心理')
  ) {
    roleCode = 'teacher'
  } else if (postName) {
    // 其他校长等角色
    roleCode = 'other' // 'other'
  }
  return roleCode
}

// 根据用户ID获取用户详情信息  isEnforce=是否强制请求（如果用户信息已经存在，则不会再次更新）
export const getUserInfo = async (
  commit: any,
  state: any,
  userId: any,
  isEnforce: boolean | undefined
) => {
  if (!state) return
  // 非强制请求，并且已存在
  if (!isEnforce && state.userInfoMap[userId]) {
    return state.userInfoMap[userId]
  }
  // 查询的用户信息是否是当前登录用户
  const isCurUser = state.user.userId === userId
  // 获取用户信息
  let reqUrl = isCurUser
    ? '/api/blade-user/info'
    : `/api/blade-user/detail?id=${userId}`
  let { data } = await axios.get(reqUrl)
  // 如果查询的用户信息是当前登录用户，则保存
  isCurUser && commit('setUserInfo', data.data)
  commit('setUserInfoMap', data.data)
  return data.data
}

// 根据家长获取对应小孩的信息
export const getUserByParent = async (commit: any, state: any) => {
  let tenantId = state.user.tenantId
  let userId = state.user.userId
  let key = tenantId + userId
  // 如果已经存在
  if (state.childrenUsers[key]) {
    return state.childrenUsers[key]
  }
  let { data } = await axios.get(
    `/api/blade-user/userRelation/list?tenantId=${tenantId}&userId=${userId}`
  )
  state.childrenUsers[key] = data.data
  commit('setChildrenUsers', {
    key: key,
    value: data.data,
  })
  return data.data
}

// 获取待办事项数量等信息
export const refreshTodoData = async (
  commit: any,
  userInfo: any,
  state?: any
) => {
  let { data } = await getTodo({
    tenantId: userInfo.tenantId,
  })
  data = (data && data.data) || {}
  let total = 0
  let roleCode = getRoleCode(userInfo)
  let dataArr = []
  let isEnterprise = state && state.enterprise.includes(userInfo.tenantId)
  // 当前角色
  if (
    roleCode === 'student' ||
    roleCode === 'parent' ||
    roleCode === 'teacher' ||
    roleCode === 'other'
  ) {
    dataArr.push({
      key: 'evaluation',
      name:
        state && state.enterprise.includes(userInfo.tenantId)
          ? '个人测评'
          : '学生测评',
      qty: data['evaluationTotal'] || 0, // 未完成的数量
    })
    total += Number(data['evaluationTotal'] || 0)
  }
  if (roleCode === 'teacher' || roleCode === 'other') {
    if (userInfo.postName !== '任课老师' && !isEnterprise) {
      dataArr.push({
        key: 'warn',
        name: '学生预警',
        qty: data['warnTotal'] || 0, // 未完成的数量
      })
      total += Number(data['warnTotal'] || 0)
    }
  }
  if (roleCode === 'teacher') {
    // dataArr.push({
    //   key: 'reserve',
    //   name: '学生预约',
    //   qty: data['markTotal'] || 0, // 未完成的数量
    // })
  }
  let op = {
    qty: total, // 总待办数量
    data: dataArr,
  }
  commit('setTodoData', op)
  return op
}

// 去测评 isBackRouter= 测评页面左上角按钮，点击后是否回退路由
export const toEvaluation = function (
  item: any,
  router: any,
  state: any,
  isEdit: any,
  isBackRouter: any
) {
  const id = item.projectId
  const userProjectId = item.userProjectGaugeId
  const gaugeTypeId = item.gaugeTypeId
  if (state.user && state.user.tenantId && state.user.tenantId != 'undefined') {
    // 去测评
    if (isEdit) {
      router.push({
        name: 'Details',
        query: {
          projectId: id,
          gaugeTypeId: gaugeTypeId,
          isBackRouter: isBackRouter,
        },
      })
    }
    // 查看测评报告
    else {
      router.push({
        name: 'EvaluateReport',
        query: { userProjectId: userProjectId, isBackRouter: isBackRouter },
      })
    }
  } else {
    Dialog.confirm({
      title: '温馨提示',
      message: '您当前未登录，确认登录后可进行测评！',
    })
      .then(() => {
        router.push({
          name: 'Login',
          params: {},
        })
      })
      .catch(() => {
        // on cancel
      })
  }
}
